﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
using Gift.Model;

namespace Gift.DAL
{
	/// <summary>
	/// 数据访问类CardType。
	/// </summary>
	public class DALCardType
	{
		public DALCardType()
		{}
		#region  成员方法

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "tbCardType"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int CardTypeId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tbCardType");
			strSql.Append(" where CardTypeId=@CardTypeId ");
			SqlParameter[] parameters = {
					new SqlParameter("@CardTypeId", SqlDbType.Int,4)};
			parameters[0].Value = CardTypeId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        public bool IsDelete(string id)
        {
            string sSQL = " select 1 from DB_Card where cardtypeid = " + id.ToString();
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL);
            if (dr.Read())
            {
                dr.Close();
                return false;
            }
            dr.Close();
            return true;
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(tbCardType model)
		{
           
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tbCardType(");
            strSql.Append("Name,Description,PhotoUrl,Mony,ProfessionAreaTypeId)");
			strSql.Append(" values (");
			strSql.Append("@Name,@Description,@PhotoUrl,@Mony,@ProfessionAreaTypeId)");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,100),
					new SqlParameter("@Description", SqlDbType.VarChar,500),
					new SqlParameter("@PhotoUrl", SqlDbType.VarChar,200),
					new SqlParameter("@Mony", SqlDbType.Decimal,9),
                                        new SqlParameter("@ProfessionAreaTypeId", SqlDbType.Int,4)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.Description;
			parameters[2].Value = model.PhotoUrl;
			parameters[3].Value = model.Mony;
            parameters[4].Value = model.ProfessionAreaTypeId;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(tbCardType model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tbCardType set ");
			strSql.Append("Name=@Name,");
			strSql.Append("Description=@Description,");
			strSql.Append("PhotoUrl=@PhotoUrl,");
            strSql.Append("OnSale=@OnSale,");
			strSql.Append("Mony=@Mony");
			strSql.Append(" where Id=@CardTypeId ");
			SqlParameter[] parameters = {
					new SqlParameter("@CardTypeId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,100),
					new SqlParameter("@Description", SqlDbType.VarChar,500),
                   	new SqlParameter("@PhotoUrl", SqlDbType.VarChar,200),
                     new SqlParameter("@OnSale", SqlDbType.Decimal,4),
					new SqlParameter("@Mony", SqlDbType.Decimal,9)};
			parameters[0].Value = model.CardTypeId;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.Description;
			parameters[3].Value = model.PhotoUrl;
            parameters[4].Value = model.OnSale;
			parameters[5].Value = model.Mony;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete tbCardType ");
			strSql.Append(" where Id=@Id ");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = Id;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public tbCardType GetModel(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,Name,Description,PhotoUrl,Mony,ProfessionAreaTypeId from tbCardType ");
			strSql.Append(" where Id=@Id ");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = Id;

			tbCardType model=new tbCardType();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.CardTypeId=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				model.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				model.PhotoUrl=ds.Tables[0].Rows[0]["PhotoUrl"].ToString();
				if(ds.Tables[0].Rows[0]["Mony"].ToString()!="")
				{
					model.Mony=decimal.Parse(ds.Tables[0].Rows[0]["Mony"].ToString());
				}
                if (ds.Tables[0].Rows[0]["ProfessionAreaTypeId"].ToString() != "")
                {
                    model.ProfessionAreaTypeId = int.Parse(ds.Tables[0].Rows[0]["ProfessionAreaTypeId"].ToString());
                }
				return model;
			}
			else
			{
			return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbCardType GetModelByName(string  sName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Description,PhotoUrl,Mony,ProfessionAreaTypeId from tbCardType ");
            strSql.Append(" where Name=@sName ");
            SqlParameter[] parameters = {
					new SqlParameter("@sName", SqlDbType.NVarChar,50)};
            parameters[0].Value = sName;

            tbCardType model = new tbCardType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.CardTypeId = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.PhotoUrl = ds.Tables[0].Rows[0]["PhotoUrl"].ToString();
                if (ds.Tables[0].Rows[0]["Mony"].ToString() != "")
                {
                    model.Mony = decimal.Parse(ds.Tables[0].Rows[0]["Mony"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProfessionAreaTypeId"].ToString() != "")
                {
                    model.ProfessionAreaTypeId = int.Parse(ds.Tables[0].Rows[0]["ProfessionAreaTypeId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbCardType GetModel(string CardCode,string CardPas)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Description,PhotoUrl,Mony from tbCardType ");
            strSql.Append(" where cardcode=@CardCode and cardpas = @CardPas ");
            SqlParameter[] parameters = {
					new SqlParameter("@CardCode", SqlDbType.NVarChar,150)
                    ,new SqlParameter("@CardPas", SqlDbType.NVarChar,150)};
            parameters[0].Value = CardCode;
            parameters[1].Value = CardPas;

            tbCardType model = new tbCardType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.CardTypeId = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.PhotoUrl = ds.Tables[0].Rows[0]["PhotoUrl"].ToString();
                if (ds.Tables[0].Rows[0]["Mony"].ToString() != "")
                {
                    model.Mony = decimal.Parse(ds.Tables[0].Rows[0]["Mony"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere,string companyId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select distinct CT.Id,CT.[Name] ");
			strSql.Append(" FROM tbCardType CT join db_card C on CT.Id = C.CardTypeId ");
           
            if (!string.IsNullOrEmpty(companyId))
            {
                strSql.Append(" join tbCompanyCardType CCT on CT.Id = CCT.CardTypeId Join tbCompany CY on CY.Id = CCT.CompanyId ");
                strSql.Append(" and CY.Id = " + companyId);
            }
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM tbCardType  ");

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int professAreaTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select C.Id,C.OnSale,C.Name as CardTypeName,C.Description,C.PhotoUrl,C.Mony,P.Name as ProfessionAreaTypeName ");
            strSql.Append(" FROM tbCardType C join tbProfessionAreaType P on C.ProfessionAreaTypeId = P.Id and P.Id = " + professAreaTypeId.ToString());
            return DbHelperSQL.Query(strSql.ToString());
        }

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tbCardType";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  成员方法
	}
}

